<?php 
if(!defined('WD')){
	header('Location:../index.php');
}





/**
 * [alert 页面跳转]
 * @param  [string] $msg [提示信息]
 * @param  string $url [跳转的路径]
 * @return [type]      [description]
 */
function alert($msg,$url=''){
	// var_dump($url);exit;
	// 
	echo "<script>alert('$msg');";
	if($url){
		echo "window.location.href='$url';";
	}else{
		echo "window.history.go(-1);";
	}
	echo "</script>";
}




/**
 * 
 * 字符截取
 * @param string $string
 * @param int $start
 * @param int $length
 * @param string $charset
 * @param string $dot
 * 
 * @return string
 */
function str_cut(&$string, $start, $length, $charset = "utf-8", $dot = '...') {
	if(function_exists('mb_substr')) {
		if(mb_strlen($string, $charset) > $length) {
			return mb_substr ($string, $start, $length, $charset) . $dot;
		}
		return mb_substr ($string, $start, $length, $charset);
		
	}else if(function_exists('iconv_substr')) {
		if(iconv_strlen($string, $charset) > $length) {
			return iconv_substr($string, $start, $length, $charset) . $dot;
		}
		return iconv_substr($string, $start, $length, $charset);
	}

	$charset = strtolower($charset);
	switch ($charset) {
		case "utf-8" :
			preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $ar);
			if(func_num_args() >= 3) {
				if (count($ar[0]) > $length) {
					return join("", array_slice($ar[0], $start, $length)) . $dot;
				}
				return join("", array_slice($ar[0], $start, $length));
			} else {
				return join("", array_slice($ar[0], $start));
			}
			break;
		default:
			$start = $start * 2;
			$length   = $length * 2;
			$strlen = strlen($string);
			for ( $i = 0; $i < $strlen; $i++ ) {
				if ( $i >= $start && $i < ( $start + $length ) ) {
					if ( ord(substr($string, $i, 1)) > 129 ) $tmpstr .= substr($string, $i, 2);
					else $tmpstr .= substr($string, $i, 1);
				}
				if ( ord(substr($string, $i, 1)) > 129 ) $i++;
			}
			if ( strlen($tmpstr) < $strlen ) $tmpstr .= $dot;
			
			return $tmpstr;
	}
}
/**
 * 获得用户的真实IP地址
 *
 * @access  public
 * @return  string
 */
function real_ip()
{
    static $realip = NULL;

    if ($realip !== NULL)
    {
        return $realip;
    }

    if (isset($_SERVER))
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip)
            {
                $ip = trim($ip);

                if ($ip != 'unknown')
                {
                    $realip = $ip;

                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
        {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }
        else
        {
            if (isset($_SERVER['REMOTE_ADDR']))
            {
                $realip = $_SERVER['REMOTE_ADDR'];
            }
            else
            {
                $realip = '0.0.0.0';
            }
        }
    }
    else
    {
        if (getenv('HTTP_X_FORWARDED_FOR'))
        {
            $realip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_CLIENT_IP'))
        {
            $realip = getenv('HTTP_CLIENT_IP');
        }
        else
        {
            $realip = getenv('REMOTE_ADDR');
        }
    }

    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

    return $realip;
}

/**
 * [dump 预格式数据]
 * @param  [array] $data [数据]
 * @return [type]       [description]
 */
function dump($data){
	echo "<pre>";
	print_r($data);
	echo "</pre>";
}







/**
 * [upload description]
 * @param  [string]  $name [文件域的name名]
 * @param  integer $size [允许上传的文件大小]
 * @param  array   $arr  [允许上传的文件类型]
 * @param  string  $dir  [文件上传的保存路径]
 * @return [array]        [文件上传的信息]
 *         					error  1   失败
 */
if(!function_exists('upload')){
	function upload($name,$dir='uploads',$size=2048576,$arr=array('jpg','png','gif')){
		if($_FILES[$name]['error']>0){
			switch($_FILES[$name]['error']){
				case 1:
					$res['error'] = 1;
					$res['msg'] = "文件超过2M";
					return $res;
					break;
				case 2:
					$res['error'] = 1;
					$res['msg'] = "文件超出MAX_FILE_SIZE大小";
					return $res;
					break;
				case 3:
					$res['error'] = 1;
					$res['msg'] = "文件上传失败";
					return $res;
					break;
				case 4:
					$res['error'] = 1;
					$res['msg'] = "请选择文件";
					return $res;
					break;
				default:
					$res['error'] = 1;
					$res['msg'] = "未知错误";
					return $res;
					break;
			}
		}

		if($_FILES[$name]['size']>$size){
			$res['error'] = 1;
			$res['msg'] = "上传文件超出指定大小";
			return $res;
		}

		$ext = pathinfo($_FILES[$name]['name'],PATHINFO_EXTENSION);
		if(!in_array($ext,$arr)){
			$res['error'] = 1;
			$res['msg'] = "请上传指定文件类型";
			return $res;
		}

		
		// $tmp = md5(time());
		// $return_tmp = '';
		// for($i=0;$i<4;$i++){
		// 	$return_tmp.='/'.$tmp[$i];
		// 	$dir .= $return_tmp;
		// }
		// if(!is_dir($dir)){
		// 	mkdir($dir,0777,true);
		// }
		// $base = substr($tmp,4);
		// $return_file = ltrim($return_tmp.'/'.$base.'.'.$ext,'/');
		// $file = $dir.'/'.$base.'.'.$ext;
		$tmp_dir = date('Y/m/d');
		$dir = $dir.'/'.$tmp_dir;
		if(!is_dir($dir)){
			mkdir($dir,0777,true);
		}
		do{
			$filename = time().rand(1000,9999);
			$file = $dir.'/'.$filename.'.'.$ext;
		}while(is_file($file));
		$return_file = $tmp_dir.'/'.$filename.'.'.$ext;

		if(is_uploaded_file($_FILES[$name]['tmp_name'])){
			move_uploaded_file($_FILES[$name]['tmp_name'], $file);
			$res['error'] = 2;
			$res['msg'] = "上传成功";
			$res['path'] = $return_file;

			return $res;
		}
	}
}
function thumb($filename,$dir,$son_width=100,$son_height=100){
	$info = getimagesize($filename);

	//获取图片的宽
	$width = $info[0];
	//获取图片的高
	$height = $info[1];

	//打开图片
	if($info[2]==1){
		$parent = imagecreatefromgif($filename);
	}else if($info[2]==2){
		$parent = imagecreatefromjpeg($filename);
	}else if($info[2]==3){
		$parent = imagecreatefrompng($filename);
	}


	// 等比例缩放
	// $son_height = ceil(($height*$son_width)/$width);

	//w/h = s_w/s_h
	//新建图像
	$son = imagecreatetruecolor($son_width,$son_height);


	imagecopyresized($son,$parent,0,0,0,0,$son_width,$son_height,$width,$height);
	$ext = pathinfo($filename,PATHINFO_EXTENSION);
	$tmp_dir = date('Y/m/d');
	$dir = $dir.'/'.$tmp_dir;
	if(!is_dir($dir)){
		mkdir($dir,0777,true);
	}
	$pathname = time().mt_rand(1000,9999).'.'.$ext;



	if($info[2]==1){
		imagegif($son,$dir.'/'.$pathname);
	}else if($info[2]==2){
		imagejpeg($son,$dir.'/'.$pathname);
	}else if($info[2]==3){
		imagepng($son,$dir.'/'.$pathname);
	}

	imagedestroy($son);
	imagedestroy($parent);
	return array('path'=>$tmp_dir.'/'.$pathname);
}
function islogin(){
	session_start();
	if(!isset($_SESSION['admin_login']) || $_SESSION['admin_login']!=1){
		alert('请登入','login.php');
	}
}